﻿
using System.Management;
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace BatteryNotify
{
	public partial class BatteryForm : Form
	{
		PowerStatus power = SystemInformation.PowerStatus;

		public BatteryForm()
		{
			InitializeComponent();
			//CheckBattery_Win32();

			float secondsRemaining = power.BatteryLifePercent;
			if (secondsRemaining >= 0)
			{
				ChargeRemaining.Text = (secondsRemaining * 100).ToString() + "% available.";
			}
			else
			{
				ChargeRemaining.Text = string.Empty;
			}
		}


		private void RefreshStatus()
		{
			int powerPercent = (int)(power.BatteryLifePercent * 100);
			if (powerPercent <= 100) BatteryIndicator.Value = powerPercent; 
				else BatteryIndicator.Value = 0; 
			switch (power.PowerLineStatus)
			{
				case PowerLineStatus.Online:
					groupBox1.Text = "Running On Power"; 
					float secondsRemaining = power.BatteryLifePercent; 
					if (secondsRemaining >= 0)
					{
						ChargeRemaining.Text = (secondsRemaining * 100).ToString() + "% available.";
						if (secondsRemaining <= 10)
							BatteryIndicator.ForeColor = Color.Red;
						else
							BatteryIndicator.ForeColor = Color.Green;
					}
					else
					{
						ChargeRemaining.Text = string.Empty;
					}
					BatteryStatus.Text = power.BatteryChargeStatus.ToString();
					break;

				case PowerLineStatus.Offline:
					groupBox1.Text = "Running On Battery";
					BatteryStatus.Text = power.BatteryChargeStatus.ToString() + " - " 
						+ (power.BatteryLifeRemaining/60).ToString() + " mins";
					break;

				case PowerLineStatus.Unknown:
					break;
			}
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			RefreshStatus();
		}

		private void BatteryForm_Load(object sender, EventArgs e)
		{
			RefreshStatus();
			timer1.Enabled = true;
		}

		public void CheckBattery()
		{
			lblBatteryInfor.Text = string.Empty;

			Type t = typeof(PowerStatus);
			PropertyInfo[] pi = t.GetProperties();

			for (int i = 0; i < pi.Length; i++)
				lblBatteryInfor.Text += pi[i].Name + ":" + pi[i].GetValue(SystemInformation.PowerStatus, null).ToString() + "\n";


			if (lblBatteryInfor.Text == string.Empty)
				lblBatteryInfor.Text = "No information";
		}

		public void CheckBattery_Win32()
		{
			System.Management.ObjectQuery query = new ObjectQuery("Select * FROM Win32_Battery");
			ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

			ManagementObjectCollection collection = searcher.Get();

			lblBatteryInfor.Text = string.Empty;

			foreach (ManagementObject mo in collection)
			{
				foreach (PropertyData property in mo.Properties)
				{
					lblBatteryInfor.Text += string.Format( "Property {0}: Value is {1}", property.Name, property.Value);
					lblBatteryInfor.Text += "\n";
				}
			}

			if (lblBatteryInfor.Text == string.Empty)
				lblBatteryInfor.Text = "No information";
		}
	}
}
